package com.march.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * 短信发送工具类
 */
public class SMSUtils2 {

	public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
		com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
				// 必填，您的 AccessKey ID
				.setAccessKeyId(accessKeyId)
				// 必填，您的 AccessKey Secret
				.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new com.aliyun.dysmsapi20170525.Client(config);
	}
	public static SendSmsResponse sendMessage(String signName, String templateCode,String phoneNumbers,String param) throws Exception {

		com.aliyun.dysmsapi20170525.Client client = SMSUtils2.createClient("LTAI5tBbvXxNS4ykMx8mNmCk", "8STPFJnbho2kuv8egAg5UBbxt8aSZa");
		com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
				.setPhoneNumbers(phoneNumbers)
				.setTemplateCode(templateCode)
				.setTemplateParam("{\"code\":\""+param+"\"}")
				.setSignName(signName);
		SendSmsResponse sendSmsResponse = null;
		try {
			// 复制代码运行请自行打印 API 的返回值
			sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
		} catch (TeaException error) {
			// 如有需要，请打印 error
			String s = Common.assertAsString(error.message);
			System.out.println(s);
		} catch (Exception _error) {
			TeaException error = new TeaException(_error.getMessage(), _error);
			// 如有需要，请打印 error
			com.aliyun.teautil.Common.assertAsString(error.message);
		}
		return sendSmsResponse;
	}
}
